package top.mable.rabbitmq.controller;


import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.mable.rabbitmq.dto.MqMsg;
import top.mable.rabbitmq.listener.DeadLetterMQ;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author wen
 * @Date 2021/7/6
 **/
@Slf4j
@RestController
public class MQController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private DeadLetterMQ deadLetterMQ;


    /**
     * 普通队列
     * @param map
     */
    /*@RequestMapping("/send")
    public void send(@RequestBody Map<String,String> map){
        MqMsg msg = new MqMsg();
        msg.setId(UUID.fastUUID().toString());
        msg.setContent(JSONUtil.toJsonStr(map));
        msg.setClassName("MQController");
        msg.setMethod("send");
        msg.setRoutingKey("demo.demo");
        msg.setExchange("demo-exchange");
        rabbitTemplate.convertAndSend("demo-exchange","demo.demo",msg);
    }*/


    /**
     * 测试延时队列
     * @param map
     */
    @RequestMapping("/testTTL")
    public void testTTL(@RequestBody Map<String,Object> map){
        log.info("testTTL----------------");

        HashMap<String,Object> args = (HashMap) map;
        String productId = (String) args.get("productId");
        String userId = (String) args.get("userId");

        MqMsg msg = new MqMsg();
        msg.setMethod("testTTL-----------------");
        msg.setId(UUID.fastUUID().toString());
        //msg.setContent(JSONUtil.toJsonStr(map));
        msg.setRoutingKey("ttl");
        msg.setExchange("ttl_direct_exchange_wen");

        msg.setForwardExchange("rabbitMqExchange");
        msg.setForwardRoutingKey("print");
        rabbitTemplate.convertAndSend("ttl_direct_exchange_wen","ttl",msg);

    }

    @RequestMapping("/testDeadMq")
    public void testDeadMq(@RequestBody Map<String,Object> map){
        log.info("testDeadMq----------------");


        MqMsg msg = new MqMsg();
        msg.setMethod("testDeadMq-----------------");
        msg.setId(UUID.fastUUID().toString());
        //msg.setContent(JSONUtil.toJsonStr(map));
        msg.setRoutingKey("businessA");
        msg.setExchange("rabbitMqExchange");

        /*msg.setForwardRoutingKey("rabbitMqExchange");
        msg.setForwardRoutingKey("print");*/
        rabbitTemplate.convertAndSend("rabbitMqExchange","businessA",msg);

    }


    @RequestMapping("/sendQueueA")
    public void sendQueueA(@RequestBody Map<String,Object> map){

        log.info("sendQueueA===>");
        deadLetterMQ.sendQueueA(map);
    }
}
